/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity.redis.hash;

import db.DBContext;
import db.RedisKey;
import java.util.Map;
import share.PGHelper;

/**
 *
 * @author Salm
 */
public class DataHash {
    protected final RedisKey redisKey;

    public DataHash(RedisKey rKey) {
        this.redisKey = rKey;
    }
    
    public String getData(String dataID)
    {
        String data = DBContext.Redis().hget(redisKey, dataID);
        return (data != null)?data:"";
    }
    
    public int getIntData(String dataID)
    {
        return PGHelper.toInt(getData(dataID));
    }
    
    public void setData(String dataID, Object data)
    {
        DBContext.Redis().hset(redisKey, dataID, String.valueOf(data));
    }
    
    public void incData(String dataID, int by)
    {
        DBContext.Redis().hincrby(redisKey, dataID, by);
    }
    
    public void remData(String... dataIDs)
    {
        DBContext.Redis().hdel(redisKey, dataIDs);
    }

    public Map<String, Object> buildAMF()
    {
        return (Map) DBContext.Redis().hgetall(redisKey);
    }
    
    public Object dump()
    {
        return buildAMF();
    }
    
    public void destroy()
    {
        DBContext.Redis().del(redisKey);
    }
}
